import time
import unittest
from util.HTMLTestRunner import HTMLTestRunner
from util.Util import BasePage


class TestRunner:

    def runner(self):
        # 创建测试套件
        suite = unittest.TestSuite()
        # 添加测试用例
        suite.addTests(unittest.TestLoader().discover(r'..\test_script', pattern='test_*.py'))
        # 创建报告文件
        timestamp = time.strftime('%Y-%m-%d_%H-%M-%S')
        path = r'../report/report_%s.html' % timestamp
        report = open(path, mode='wb')
        # 创建用例运行器
        test_runner = HTMLTestRunner(stream=report, title='然之自动化测试报告', description='报告的详细内容....')
        # 运行用例，生成报告
        test_runner.run(suite)
        # 发送报告
        BasePage.send_mail(path, 'smtp.qq.com', '1131896900@qq.com', 'kgdyrppdjukphhcc',
                           '1131896900@qq.com', 'Ranzhi自动化测试报告？', '本文件接收人请好好查看测试报告？')


if __name__ == '__main__':
    TestRunner().runner()
